var DPOverlay = function ( params ){
	this.options = this.extend({
		// CSS classes
		classOverlay:        'dp_jsoverlay',
		classOverlayer:      'dp_jsoverlayer',
		classOverlayContent: 'dp_jsoverlayer_content',
		
		// margins
		marginTop: 20,
		
		// ids
		overlayID:   'dp_overlay',
		overlayerID: 'dp_overlayer'
	}, params);
	
	// create ID
	var z = 0;
	for(var i=0; i<100; i++){
		z = z + (i*2);
		if( jQuery('#'+this.options.overlayID+"_"+i).size() === 0 ){
			this.options.overlayID   = this.options.overlayID+"_"+i;
			this.options.overlayerID = this.options.overlayerID+"_"+i;
			break;
		}else{
			z = parseInt(jQuery('#'+this.options.overlayID+"_"+i).css('z-index'));
		}
	}
	
	// add layers
	jQuery('body').prepend('<div id="'+this.options.overlayerID+'" class="'+this.options.classOverlayer+'">'
		+'<div class="'+this.options.classOverlayContent+'"></div>'
		+'</div>');
	jQuery('body').prepend('<div id="'+this.options.overlayID+'" class="'+this.options.classOverlay+'"></div>');
	
	// set z-index
	if( z > 0 ){
		jQuery('#'+this.options.overlayID).css('z-index', z);
		jQuery('#'+this.options.overlayerID).css('z-index', (z+1));
	}
};

// methods
DPOverlay.prototype = {
	extend: function( destination, source ) {
		if( destination && source ){
			for( var property in source ){
				var value = source[property];
				if( value !== undefined ){
					destination[property] = value;
				}
			}
			
			// Fix toString() in IE don't try if FF/Windows < 2.0.0.13
			var sourceIsEvt = typeof window.Event == "function" && source instanceof window.Event;
			if( !sourceIsEvt && source.hasOwnProperty && source.hasOwnProperty('toString') ){
				destination.toString = source.toString;
			}
		}
		return destination;
	},
	
	getViewportSize: function (  ) {
		var e = window
			,a = 'inner';
		if( !('innerWidth' in window) ){
			a = 'client';
			e = document.documentElement || document.body;
		}
		return {
			width : e[ a+'Width' ],
			height : e[ a+'Height' ]
		};
	},
	
	getViewportPosition: function (  ) {
		var e = document.documentElement || {},
			b = document.body || {},
			w = window;
		
		return {
			x: w.pageXOffset || e.scrollLeft || b.scrollLeft || 0,
			y: w.pageYOffset || e.scrollTop || b.scrollTop || 0
		};
	},
	
	setCSS: function( css ) {
		jQuery('#'+this.options.overlayerID).css(css);
	},
	
	setContent: function( content, align ) {
		jQuery('#'+this.options.overlayerID).find('div.'+this.options.classOverlayContent).html(content);
		if( align !== false ){
			this.alignToViewport();
		}
	},
	
	alignToViewport: function(  ) {
		//console.log(this.getViewportSize());
		//console.log(this.getViewportPosition());
		var y = parseInt(this.getViewportPosition().y);
		this.setCSS({top: y+this.options.marginTop+'px'});
	},
	
	remove: function(  ) {
		jQuery('#'+this.options.overlayerID).remove();
		jQuery('#'+this.options.overlayID).remove();
	}
};